package com.yequan.spring.lookup;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 使用 {@link ObjectProvider} 进行依赖查找
 *
 * @author : Administrator
 * @date : 2020/4/9
 */
public class ObjectProviderDemo {//@Configuration 非必须


    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(ObjectProviderDemo.class);

        applicationContext.refresh();

        //通过 ObjectProvider
        lookupByObjectProvider(applicationContext);

        applicationContext.close();

    }

    @Bean
    public String helloWorld() {
        return "Hello,World";
    }

    /**
     * 通过 ObjectProvider
     *
     * @param applicationContext
     */
    private static void lookupByObjectProvider(AnnotationConfigApplicationContext applicationContext) {
        ObjectProvider<String> beanProvider = applicationContext.getBeanProvider(String.class);
        System.out.println(beanProvider.getObject());
    }

}
